#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    bool canPartition(vector<int>& nums) {
        int m = nums.size();
        int sum = 0;
        for (auto x : nums) sum += x;
        if (sum % 2 == 1) return false;
        int n = sum / 2;
        vector<vector<bool>> dp(m + 1, vector<bool>(n + 1));
        dp[0][0] = true;
        for (int i = 1;i <= m;i++) dp[i][0] = true;
        for (int i = 1;i <= m;i++)
        {
            for (int j = 1;j <= n;j++)
            {
                dp[i][j] = dp[i - 1][j];
                if (j >= nums[i - 1]) dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i - 1]];
            }
        }
        return dp[m][sum / 2];
    }
};